package com.xiezc.common.result;

import lombok.Data;

import java.util.List;

@Data
public class PageResult<T> extends BaseResult {

    List<T> records;

    Long current;

    Long pageSize;

    Long totalItem;

    Long totalPage;

    public static <T> PageResult<T> success(List<T> records, Long current,
                                            Long pageSize, Long totalItem, Long totalPage) {

        PageResult<T> pageResult = new PageResult<>();
        pageResult.setCurrent(current);
        pageResult.setPageSize(pageSize);
        pageResult.setRecords(records);
        pageResult.setTotalItem(totalItem);
        pageResult.setTotalPage(totalPage);
        return pageResult;
    }

    public static <T> PageResult<T> success(List<T> records, Long current,
                                            Long pageSize, Long totalItem) {

        PageResult<T> pageResult = new PageResult<>();
        pageResult.setCurrent(current);
        pageResult.setPageSize(pageSize);
        pageResult.setRecords(records);
        pageResult.setTotalItem(totalItem);
        long totalPage = totalItem / pageSize;
        if (totalItem % pageSize != 0) {
            totalPage++;
        }
        pageResult.setTotalPage(totalPage);
        return pageResult;
    }
}
